package com.vvlive.caibao.smartcity.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhj on 2015/9/25.
 * 设置工具类
 */
public class SettingsUtil {

    private static final String SETTINGS_NAME = "sc_settings";
    private static final String DEFAULT_MEETING_IP = "192.168.0.115:8080/";
    public static final String KEY_IP = "key_ip";

    public static final String KEY_USERID = "userid";
    public static final String KEY_PASSWORD = "password";

    private SharedPreferences mPreferences;

    public SettingsUtil(Context context) {
        mPreferences = context.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE);
    }

    public boolean editMeetingIP(String newIP) {
        return mPreferences.edit().putString(KEY_IP, "http://" + newIP).commit();
    }

    public String getMeetingIP() {
        return mPreferences.getString(KEY_IP, "http://" + DEFAULT_MEETING_IP);
    }

    /**
     * 保存用户
     *
     * @param userid
     * @param password
     * @return
     */
    public boolean saveUser(String userid, String password) {
        return mPreferences.edit().putString(KEY_USERID, userid)
                       .putString(KEY_PASSWORD, password).commit();
    }

    /**
     * 获取用户的基本信息（用户名，密码）
     *
     * @return
     */
    public Map<String, String> getUserInfo() {
        Map<String, String> info = new HashMap<>();
        info.put(KEY_USERID, mPreferences.getString(KEY_USERID, ""));
        info.put(KEY_PASSWORD, mPreferences.getString(KEY_PASSWORD, ""));
        return info;
    }
}
